<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Login
 *
 * @author ETorres
 * @export Modules
 * @export-metadata default-module
 */
class Login extends Module {
    public function index() {
        $urlReturn = $this->request->get('urlReturn');
        if(isset($urlReturn)){
            $this->assign('action', $this->route->action('doLogin', array('urlReturn' => $urlReturn)));
            $this->assign('loginEgresado', $this->route->action('loginEgresado', array('urlReturn' => $urlReturn)));
        } else {
            $this->assign('action', $this->route->action('doLogin', array('urlReturn' => $this->route->appAction('Default', 'FrontPage'))));
            $this->assign('loginEgresado', $this->route->action('loginEgresado', array('urlReturn' => $this->route->appAction('Default', 'FrontPage'))));
        }
        if($this->request->get('error')) {
            $this->addMessage(new Message("Credenciales Erroneas", "", Message::ERROR));
        }
        $this->display();
    }
    
    public function loginEgresado() {
        $urlReturn = $this->request->get('urlReturn');
        if(isset($urlReturn)){
            $this->assign('action', $this->route->action('doLogin', array('urlReturn' => $urlReturn)));
            $this->assign('login', $this->route->action('index', array('urlReturn' => $urlReturn)));
        } else {
            $this->assign('action', $this->route->action('doLogin', array('urlReturn' => $this->route->appAction('Default', 'FrontPage'))));
            $this->assign('login', $this->route->action('index', array('urlReturn' => $this->route->appAction('Default', 'FrontPage'))));
        }
        
        $this->display();
    }

    public function doLogin() {
        $user = $this->request->post('user');
        
        if(isset($user)){            
            $status = $this->model->Authentication->doLogin($user);
            if($status){
                $this->route->redirect($this->request->get('urlReturn'));
            } else {
                $this->route->redirect($this->route->action('index', array('error' => 'true')));               
            }
        } else {
            $this->route->redirect($this->route->action('index'));
        }
    }
    
    public function doLogout() {        
        $this->model->Authentication->doLogout();
        $urlReturn = $this->request->get('urlReturn');
        if(isset($urlReturn)){
            $this->route->redirect($urlReturn);
        } else {
            $this->route->redirect('http://virtual.usbcartagena.edu.co/');
        }
    }
}

?>
